{{!
  Copyright (c) HashiCorp, Inc.
  SPDX-License-Identifier: BUSL-1.1
~}}

{{page-title "CSI Plugins"}}
<StorageSubnav />
<section class="section">
  {{#if this.isForbidden}}
    <ForbiddenMessage />
  {{else}}
    <div class="toolbar">
      <div class="toolbar-item">
        {{#if this.model.length}}
          <SearchBox
            data-test-plugins-search
            @searchTerm={{mut this.searchTerm}}
            @onChange={{action this.resetPagination}}
            @placeholder="Search plugins..." />
        {{/if}}
      </div>
    </div>
    {{#if this.sortedPlugins}}
      <ListPagination
        @source={{this.sortedPlugins}}
        @size={{this.pageSize}}
        @page={{this.currentPage}} as |p|>
        <ListTable
          @source={{p.list}}
          @sortProperty={{this.sortProperty}}
          @sortDescending={{this.sortDescending}}
          @class="with-foot" as |t|>
          <t.head>
            <t.sort-by @prop="plainId">ID</t.sort-by>
            <t.sort-by @prop="controllersHealthyProportion">Controller Health</t.sort-by>
            <t.sort-by @prop="nodesHealthyProportion">Node Health</t.sort-by>
            <t.sort-by @prop="provider">Provider</t.sort-by>
          </t.head>
          <t.body @key="model.id" as |row|>
            <tr class="is-interactive" data-test-plugin-row {{on "click" (action "gotoPlugin" row.model)}}>
              <td data-test-plugin-id
                {{keyboard-shortcut
                  enumerated=true
                  action=(action "gotoPlugin" row.model)
                }}
              >
                <LinkTo @route="storage.plugins.plugin" @model={{row.model.plainId}} class="is-primary">{{row.model.plainId}}</LinkTo>
              </td>
              <td data-test-plugin-controller-health>
                {{#if row.model.controllerRequired}}
                  {{if (gt row.model.controllersHealthy 0) "Healthy" "Unhealthy"}}
                  ({{row.model.controllersHealthy}}/{{row.model.controllersExpected}})
                {{else}}
                  {{#if (gt row.model.controllersExpected 0)}}
                    {{if (gt row.model.controllersHealthy 0) "Healthy" "Unhealthy"}}
                    ({{row.model.controllersHealthy}}/{{row.model.controllersExpected}})
                  {{else}}
                    <em class="is-faded">Node Only</em>
                  {{/if}}
                {{/if}}
              </td>
              <td data-test-plugin-node-health>
                {{if (gt row.model.nodesHealthy 0) "Healthy" "Unhealthy"}}
                ({{row.model.nodesHealthy}}/{{row.model.nodesExpected}})
              </td>
              <td data-test-plugin-provider>{{row.model.provider}}</td>
            </tr>
          </t.body>
        </ListTable>
        <div class="table-foot">
          <PageSizeSelect @onChange={{action this.resetPagination}} />
          <nav class="pagination">
            <div class="pagination-numbers">
              {{p.startsAt}}&ndash;{{p.endsAt}} of {{this.sortedPlugins.length}}
            </div>
            <p.prev @class="pagination-previous">
              <Hds::Icon @name="chevron-left" />
            </p.prev>
            <p.next @class="pagination-next">
              <Hds::Icon @name="chevron-right" />
            </p.next>
            <ul class="pagination-list"></ul>
          </nav>
        </div>
      </ListPagination>
    {{else}}
      <div data-test-empty-plugins-list class="empty-message">
        {{#if (eq this.model.length 0)}}
          <h3 data-test-empty-plugins-list-headline class="empty-message-headline">No Plugins</h3>
          <p class="empty-message-body">
            The cluster currently has no registered CSI Plugins.
          </p>
        {{else if this.searchTerm}}
          <h3 data-test-empty-plugins-list-headline class="empty-message-headline">No Matches</h3>
          <p class="empty-message-body">
            No plugins match the term <strong>{{this.searchTerm}}</strong>
          </p>
        {{/if}}
      </div>
    {{/if}}
  {{/if}}
</section>
